package com.shangma.demo;

import com.shangma.util.JedisUtils;
import redis.clients.jedis.*;

import java.util.*;

/**
 * @author: ShangMa
 * @className: TestDemo
 * @description:
 * @date: 2023/4/3 16:16
 * @since: 11
 */
public class TestDemo {

    public static void main(String[] args) {

        // 创建jedis连接池配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(10);

        // 连接不同的jedis服务器
        JedisShardInfo info1 = new JedisShardInfo("localhost", 6379);
        JedisShardInfo info2 = new JedisShardInfo("localhost", 7777);

        List<JedisShardInfo> list = new ArrayList<>();
        list.add(info1);
        list.add(info2);

        // jedis集群配置
        ShardedJedisPool pool = new ShardedJedisPool(config,list);

        ShardedJedis jedis = pool.getResource();
        // 测试数据
        for (int i = 0; i < 100; i++) {
            jedis.set("name" + i,"gfs" + i);
        }
        jedis.close();


    }

    private static void method2() {
        // 使用连接池来获得redis，不用时把redis对象归还到连接池中
//        // 创建jedis连接池的配置
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setMaxTotal(10); //设置最大的连接数量
//        // 创建jedis连接池对象
//        JedisPool jedisPool = new JedisPool(poolConfig,"localhost",6379);
//        // 从连接池中获取jedis对象
//        Jedis jedis = jedisPool.getResource();
//        jedis.set("aaaaaa","bbb");
//        System.out.println(jedis.get("aaaaaa"));
//
//        // 归还连接
//        jedis.close();


        Jedis jedis = JedisUtils.getJedis();
        jedis.set("afsdf","fefe");
        System.out.println(jedis.get("afsdf"));
        JedisUtils.close(jedis);
    }

    private static void method1() {
        // 创建jedis对象  默认就是localhost:6379
        Jedis jedis = new Jedis();
        // 操作数据类型

        // string
        jedis.set("akey","你好");
        System.out.println(jedis.get("akey"));
        jedis.mset("bkey","你好","ckey","您好");
        System.out.println(jedis.mget("bkey","ckey"));

        // hash
        jedis.hset("user","username","zhangsan");
        System.out.println(jedis.hget("user","username"));
        Map<String,String> map = new HashMap<>();
        map.put("name","zs");
        map.put("sex","男");
        map.put("age","18");
        jedis.hmset("user",map);
        System.out.println(jedis.hmget("user","age","sex"));


        // list
        jedis.lpush("lkey","aaa","bbb","ccc");
        System.out.println(jedis.rpop("lkey"));

        // set
        jedis.sadd("s1","a","b","c","d");
        jedis.sadd("s2","a","b","c","d","e","f");
        Set<String> set = jedis.sdiff("s2", "s1");
        set.forEach(s -> System.out.println(s));

        // sortedset
        Map<String,Double> map1 = new HashMap<>();
        map1.put("aa",100.0);
        map1.put("bb",98.9);
        jedis.zadd("zkey",map1);
        jedis.zadd("zkey2",98.7,"ffff");
//        List<String> zkey2 = jedis.zrange("zkey", 0, -1);
//        System.out.println(zkey2);


        // 释放资源
        jedis.close();
    }
}
